Rustc Parameter Optimization

Objective:


This notebook explores the possibility of selecting the right Rustc complilation flags in order to optimize for build time, run time, and binary size. This is a particularly important issue in the Rust community as Rust compilation times are notoriously long. Specifically, a selection of certain compilation flags can reproduce certain runtime or binary size requirements while minimizing compile time. This is ideal for a tight development loop. The following are the options that this demo is able to optimize in order to discover the pareto frontier:


In this demo, I focus on optimizing an old graphics test of mine done with the Rust graphics library Bevy. (In order to get this running, you may need to install xorg, openbox, and libasound2 via your package manager). This was run on x86 using Rustc 1.47.0.

Here is where I setup the optimizer and give it the parameters that it has to play with and the goals that it is optimizing for. Note, the options are passed in as integers to be converted later. This is due to certain formatting issues and pluggability requirements

This step sets up a tool that is critical for the optimizer. Some of the configurations will result in a failed compile (specifically surrounding the link time optimizer compile option), as such, the model needs to know not to keep exploring those options. Multiplying the base case by a large number (10 in this case) ensures that these points will almost certainly be worse than anything the optimizer would encounter with a successful compile

Perform 1000 iterations of getting suggestions, evaluating them to see performance, and then getting new suggestions. I save the optimizer every 100 iterations. Note: Many of these may be very fast due to selecting incompatable compilation flags

Results / Data Processing


(Make sure that you have the optimizer written to a file as this will re-load the optimizer from a file)

These are the possible options for ideal compile vs run time vs binary size

This visualization is designed to help explain the distance to the pareto frontier given certain individual parameters. The lto flag has particularly interesting interactions with opt-level and codegen-units. (The lighter the value, the worse the performance)

Conclusions

This has proven to be a very fruitful exercise as the graph of "All Observations" demonstrates that while the debug build might not be worth changing at this point in time, if you want release style performance with a lower compile time, the correct set of flags was able to save 27% on compile time while keeping equivalent run time performance. In this case, this performance was also optimal across two machines of different hardware specifications. However this may not always be the case.
If you have time, replace my bevybench rust program with your own such that when you run "cargo run", it runs a benchmark of your program. Then, let the optimizer go to town overnight and see how much time can be saved on your compilation via ideal selection of flags for your machine and for your code.